const config = require('./Configuration.js');
const getPolicyEncode = require('./getPolicy.js');
const getSignature = require('./GetSignature.js');

const OBSupload = function (filePath){
  if(!filePath){
    wx.showToast({
      title: '文件路径不能为空',
      icon: '请重新选择路径',
    });
  }
  else{
    const fileName = '001.jpg';   //指定上传到OBS桶中的对象名    

    const OBSPolicy = {                   //设定policy内容
      "expiration": "2089-12-31T12:00:00.000Z",
      "conditions": [
        {"bucket": "goin"},        //Bucket name
        { 'key': fileName}
      ]
    }

    const policyEncoded = getPolicyEncode(OBSPolicy);                    //计算policy编码值
    const signature = getSignature(policyEncoded, config.SecretKey);     //计算signature

    wx.uploadFile({
      //url: config.EndPoint,
      url: config.EndPoint,
      filePath: filePath,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data; boundary=-9431149156168',
      },
      formData: {
        'AccessKeyID': config.AccessKeyId,
        'policy': policyEncoded,
        'signature': signature,
        'key': fileName,
      },

      success: function(res){
        console.log(res.statusCode);            //打印响应状态码
        if(res.statusCode=='204'){
          console.log('上传图片成功', res)
          wx.showToast({
            title: '上传成功',
            icon: '成功'
          });
        }
        else{
          console.log('上传图片失败', res)
          wx.showToast({
            title: '上传失败',
            icon: '失败'
          });
        }
      },
      fail: function(e){
        console.log(e);
      }
    })

  }
}

module.exports = OBSupload;